package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.itheima.common.R;
import com.itheima.pojo.AddressBook;
import com.itheima.service.IAddressBookService;
import com.itheima.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    IAddressBookService addressBookService;

    //新增地址
    @PostMapping
    private R<AddressBook> addAddress(@RequestBody AddressBook addressBook){
        //1.先获取是哪个用户，要新增地址
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    //设置地址默认值
    @PutMapping("/default")
    private R<AddressBook> changeDefaultAddress(@RequestBody AddressBook addressBook){
        //1.先将所有的isdefault置为0
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        wrapper.set(AddressBook::getIsDefault,0);

        addressBookService.update(wrapper);

        //2.修改指定的默认值
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    //查询指定用户的全部地址
    @GetMapping("/list")
    private R<List<AddressBook>> selectAllAddress() {
        //1.查询是哪个用户登录了
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId,userId);

        List<AddressBook> list = addressBookService.list(wrapper);
        return R.success(list);
    }

    //根据id查询地址
    @GetMapping("/{id}")
    private R selectById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return R.success(addressBook);
        } else {
            return R.error("没有找到该对象");
        }
    }

    //查询默认地址
    @GetMapping("/default")
    private R<AddressBook> selectByDefault(){
        Long useId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getIsDefault,1l);
        wrapper.eq(AddressBook::getUserId,useId);

        AddressBook addressBook = addressBookService.getOne(wrapper);
        if (addressBook == null){
            //没有该地址
            return R.error("没有该对象");
        }
        return R.success(addressBook);
    }

    //修改地址
    @PutMapping
    private R<String> updateAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("地址修改成功");
    }

    //删除地址【可能是多个】
    @DeleteMapping
    private R<String> deleteByIds(@PathVariable Long[] ids){
        addressBookService.removeByIds(Arrays.asList(ids));
        return R.success("删除成功");
    }
}
